from google.appengine.ext import db, webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from user_profile import model
from droidguide_classes import *

class Save(webapp.RequestHandler):
    
  def get(self):
    key_name = self.request.get('key')
    user_profile = model.UserProfile()
    if key_name:
      user_profile = db.get(db.Key(key_name))
      
    user_profile.name = self.request.get('name')
    
    user_profile.droidGuideUser_key = self.request.get('droidGuideUser_key')
    print 'droidGuideUser_key %s' % user_profile.droidGuideUser_key
    user_profile.gender = self.request.get('gender')
      
    if self.request.get('birth_year'):
      user_profile.birth_year = int(self.request.get('birth_year'))
      
    user_profile.marital_status = self.request.get('marital_status')
      
    if self.request.get('food'):
      user_profile.food = bool(int(self.request.get('food')))
      
    if self.request.get('drink'):
      user_profile.drink = bool(int(self.request.get('drink')))
      
    if self.request.get('style'):
      user_profile.style = self.request.get('style')
      
    if self.request.get('consumer'):
      user_profile.consumer = int(self.request.get('consumer'))
      
    if self.request.get('historical'):
      user_profile.historical = int(self.request.get('historical'))
            
    if self.request.get('ecological'):
      user_profile.ecological = int(self.request.get('ecological'))
            
    if self.request.get('gastronomic'):
      user_profile.gastronomic = int(self.request.get('gastronomic'))
            
    if self.request.get('cultural'):
      user_profile.cultural = int(self.request.get('cultural'))
            
    if self.request.get('bohemian'):
      user_profile.bohemian = int(self.request.get('bohemian'))
      
    if self.request.get('weather'):
      user_profile.weather = bool(int(self.request.get('weather')))
      
    if self.request.get('traffic'):
      user_profile.traffic = bool(int(self.request.get('traffic')))

    db.put(user_profile)

    self.response.headers['Content-Type'] = 'text/plain'
    self.response.out.write(str(user_profile.key()) + '\n')

application = webapp.WSGIApplication([('/user_profile/save', Save)], debug=True)


def main():
  run_wsgi_app(application)

if __name__ == '__main__':
  main()
